import React from 'react';
import classNames from 'classnames';

import { message as MessageConfig } from '../config/Common';
import MessageSale from '../components/MessageSale';
import MessageBuy from '../components/MessageBuy';
import MessageSys from '../components/MessageSys';
import MessageRecommend from '../components/MessageRecommend';
import MessageConfirm from '../components/MessageConfirm';
import DialogSign from './DialogSign';
import DialogSignLong from './DialogSignLong';
import DialogUnSign from './DialogUnSign';
import { product as ProductConfig} from '../config/Common';

class Message extends React.Component {

  constructor() {
    super();

    this.state = {
      dialogVisiable : false,
      product : null
    };
  }

  static propTypes = {
      message : React.PropTypes.array,
      messagesys : React.PropTypes.array,
  }

  static defaultProps = {
      message : null,
      messagesys : null
  }

  getMessageRender(message, index){

    let { messageAction } = this.props;

    let messageResult = null;

    //买入
    if(message.ordertype === MessageConfig.messageType.buy){ 
      //中线 & 已关注 & 未确认查看 & （TODO：未过期）
      if(message.gstype === "4BBDBC2C-5FDE-40FE-8ADF-2B45C1CA7682" 
        && message.viewstatus === ProductConfig.productStatus.hasSign
        && message.id !==""
        && (message.msgconfirm !== "2")){
        messageResult =  (
           <MessageConfirm
            message={message} 
            key = {index}
            action = { messageAction } 
            handleLongMessageView = { this.handleLongMessageView.bind(this, message) } />
        );
      }else{
        messageResult =  (
           <MessageBuy 
            message={ message } 
            key = {index}
            action = { messageAction } 
            handleProductOp = { this.handleProductOp.bind(this, message) } />
        );
      }
    }
    //卖出
    else if(message.ordertype === MessageConfig.messageType.sale){
      messageResult = (
         <MessageSale 
          message={message} 
          key = {index}
          action = { messageAction }
          handleProductOp= { this.handleProductOp.bind(this, message) }/>
      );
    }
    //盘前股讯消息
    else if(message.data && message.data.length){
      messageResult = (
         <MessageRecommend message={message} key = {index}/>
      );
    }else{
      messageResult = (
         <MessageSys message={message} key = {index}/>
      );
    }
    return messageResult;
  }

  getProductById(pid){
    let { products } = this.props;
    let productInfo = {};
    if(products && products.results && products.results.length){
      for(let i =0 ,len = products.results.length; i < len; i++){
        let productItem = products.results[i];
        if (productItem.gsid === pid) {
          productInfo = productItem;
          break;
        }
      }
    }
    return productInfo;
  }

  handleLongMessageView(message, event){
    if (message && message.id) {
      let { messageAction } = this.props;
      //消息确认查看
      messageAction.confirmMessage(message.id);
    }
  }

  handleProductOp(message, event){
    if (message && message.gsid) {
      let productInfo = this.getProductById(message.gsid);

      if (productInfo && productInfo.gsid) {
        this.setState({
          dialogVisiable : true,
          product : productInfo
        });
      }
    }

    event.preventDefault();
  }

  handleDialogHide(){
    this.setState({
       dialogVisiable : false
    });
  }

  render() {

    let { messages, productAction, user, history } = this.props;

    return (
        <div>
           { messages && messages.map((message, i) => 
             {
                return (
                  this.getMessageRender(message, i)
                )
             }
           )}

           {  
              this.state.product && this.state.product.status === ProductConfig.productStatus.hasSign?
              <DialogUnSign 
                product = { this.state.product } 
                productAction = { productAction }
                user = { user }
                history = { history }
                visiable = { this.state.dialogVisiable } 
                onHide = { this.handleDialogHide.bind(this) }/>
              :
              this.state.product && this.state.product.gstype === ProductConfig.productType.long?
              <DialogSignLong 
                product = { this.state.product }
                productAction = { productAction }
                user = { user }
                history = { history }
                visiable = { this.state.dialogVisiable } 
                onHide = { this.handleDialogHide.bind(this) }/>
              :
              <DialogSign
                product = { this.state.product }
                productAction = { productAction }
                user = { user }
                history = { history }
                visiable = { this.state.dialogVisiable } 
                onHide = { this.handleDialogHide.bind(this) }/>
           } 
        </div>
    );
  }
}

export default Message;
